package com.hongsheng.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hongsheng.pojo.Depository;
import com.hongsheng.pojo.R;
import com.hongsheng.service.DepositoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * TODO
 *
 * @author admin
 * @version 1.0
 * @date 2023/8/11 16:24
 */
@RestController
@CrossOrigin
@RequestMapping("/depository")
public class DepositoryController {
    @Resource
    private DepositoryService depositoryService;
    //查询所有仓库
    @GetMapping()
    public R findAll(){
        List<Depository> allDepository = depositoryService.findAllDepository();
        for (Depository depository : allDepository) {
            System.out.println(depository);
        }
        return new R(true,allDepository);
    }
    //根据id删除仓库
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id){
        Boolean flag = depositoryService.deleteDepository(id);
        return new R(flag);
    }
    //编辑仓库
    @PutMapping
    public R update(@RequestBody Depository depository){
        Boolean flag = depositoryService.updateDepository(depository);
        return new R(flag);
    }
    //添加仓库
    @PostMapping
    public R insert(@RequestBody Depository depository){
        Boolean flag = depositoryService.insertDepository(depository);
        return new R(flag);
    }
    //根据id查询仓库
    @GetMapping("/{id}")
    public R findById(@PathVariable Integer id){
        return new R(true,depositoryService.selectDepositoryById(id));
    }
    @GetMapping("/{currentPage}/{pageSize}")
    public R getAll(@PathVariable Integer currentPage,@PathVariable Integer pageSize,Depository depository){
        IPage<Depository> page = depositoryService.getPage(currentPage, pageSize,depository);
        if (currentPage>page.getPages()){
            page=depositoryService.getPage((int) page.getPages(),pageSize,depository);
        }
        return new R(true,page);
    }
    //查询仓库是否存在货物
    @GetMapping("/existGoods/{id}")
    public R existGoods(@PathVariable Integer id){
        return new R(true,depositoryService.existGoods(id));
    }

    @GetMapping("/findPhone/{ManagerPhone}")
    public R findPhone(@PathVariable String ManagerPhone){
        Boolean phone = depositoryService.findPhone(ManagerPhone);
        return new R(true,phone);
    }

}
